Skip to content

feat(read): support predicates on non-projected fields - #599

Open
lszskye wants to merge 3 commits into
apache:mainfrom
lszskye:unbind_predicate_field_in_projection
Open

lszskye wants to merge 3 commits into
apache:mainfrom
lszskye:unbind_predicate_field_in_projection

Conversation

@lszskye

@lszskye lszskye commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Purpose

Remove the requirement that predicate fields must appear in the output projection. For example, a read can now return only name while filtering on age, without exposing age in the result. This also applies to full-text predicate fields.

Introduce ReadSchemaPlan to separate the requested output schema from the internal schema containing predicate and authorization dependencies. Use ProjectionBatchReader to remove these dependencies after filtering, merging, or masking. Apply this consistently across append, primary-key, data-evolution, realtime, precise-index, query-auth, and format-table reads.

The change also addresses several predicate-handling issues:

  • AbstractSplitRead: Pass only predicates on fields present in the data schema to file-index evaluation, excluding virtual system fields. Preserve the original predicate for subsequent reading and filtering.
  • CompleteRowTrackingFieldsBatchReader: Stop pushing predicates on missing _ROW_ID and _SEQUENCE_NUMBER columns into the physical format reader. Keep these predicates for the existing filtering stage after the values are completed. Predicates on columns already present in the file remain eligible for pushdown.
  • FileStoreScan: Exclude predicates requiring virtual row-tracking fields from manifest-statistics pruning. Drop the entire affected conjunct, including mixed OR expressions, to avoid incorrectly pruning matching files.
  • Predicate validation: Remove field-index equality checks while retaining field existence, type, and literal validation. Bind execution positions by field name so existing callers using projection-relative indices remain compatible.

Tests

Add or update coverage for:

  • Unprojected data and partition predicates in append and primary-key reads, including schema evolution.
  • Data-evolution, precise scalar/full-text index reads, score output, and realtime disk/memory reads.
  • Realtime deduplication and primary-key merging before filtering and projection.
  • Query-auth predicates on unprojected masked columns and format-table predicates on unprojected columns.
  • Projection behavior with selection bitmaps, dictionary arrays, metadata, nulls, and empty projections.

API and Format

No public API signature, storage format, or protocol changes.

Documentation

Update predicate API comments and the file-index, realtime, and format-table guides to describe predicates on non-projected fields.

Generative AI tooling

Generated-by: OpenAI Codex (GPT-6)

@SteNicholas SteNicholas left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lszskye, I left two comments for read correctness issues. PTAL.

@SteNicholas SteNicholas left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lszskye, I left some comments on the GCC build failure and the sequence-number predicate behavior in append reads and primary-key merges. PTAL.

}
if (core_options.RowTrackingEnabled() || core_options.KeyValueSequenceNumberEnabled()) {
allowed_system_fields.push_back(
DataField::ConvertDataFieldToArrowField(SpecialFields::SequenceNumber()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Evaluate _SEQUENCE_NUMBER after primary-key merging. GenerateKeyPredicates() excludes non-key names from the table schema, so the virtual _SEQUENCE_NUMBER survives the key-only filter and reaches the format reader for overlapping runs. I reproduced this with two Parquet files holding the same key. A predicate matching the older sequence returned val=10 even though the latest row had val=20 and should have been removed by the post-merge filter. Build the pre-merge predicate from the trimmed primary-key allowlist, preserve the sequence predicate for the merged row, and cover the two-file case.

allowed_system_fields.push_back(
DataField::ConvertDataFieldToArrowField(SpecialFields::RowId()));
}
if (core_options.RowTrackingEnabled() || core_options.KeyValueSequenceNumberEnabled()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Require a primary key for the key-value sequence-number option. Both scan and read validation accept _SEQUENCE_NUMBER on an append table with key-value.sequence_number.enabled=true and row tracking disabled. I reproduced a successful TableRead::Create() followed by NextBatch() failing because _SEQUENCE_NUMBER does not exist in the Parquet file. Use the same availability check at both entry points and cover the rejection on an append table alongside the supported primary-key case.

PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::StructArray> output,
arrow::StructArray::Make(arrays, output_schema->fields()));
auto output = std::make_shared<arrow::StructArray>(arrow::struct_(output_schema->fields()),
data->length(), arrays);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d like to understand what this change is for.

const auto& materialization_schema = context_->EnablePreciseFileIndexEvaluate()
? context_->GetReadSchemaPlan()->GetOutputSchema()
: read_schema;
auto create_reader =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest consolidating EnablePreciseFileIndexEvaluate as much as possible. Right now it’s being special-cased in multiple places, including read_schema, predicate handling, and CreateReader.

std::move(inner_reader),
context_->GetReadSchemaPlan()->GetOutputSchema()->field_names(), arrow_pool_);
}
return inner_reader;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part feels a bit odd. If you’re constantly rewriting, shouldn’t it no longer be called the inner reader? It seems like you’re returning an inner_reader, and the outer layer should wrap it as well—right?

/// -> CompleteRowKindBatchReader -> (PredicateBatchReader) -> ConcatBatchReader across files
/// -> (ProjectionBatchReader) -> CompleteRowKindBatchReader -> (PredicateBatchReader)
/// -> ConcatBatchReader across files
/// -> (CompleteIndexScoreFileBatchReader) -> FieldMappingReader

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m a bit curious—why can’t ProjectionBatchReader be placed last, with TopKBatchReader before? That way, we wouldn’t need any special handling for the score field. Also, the setup in GetProjectionFieldNames: it doesn’t return the set of user-projected field names; it includes score-related fields, which is very easy to misunderstand. For example, many times I thought the logic of GetProjectionFieldNames should be something like schema_plan_->GetOutputSchema()->field_names().

std::move(batch_reader),
context_->GetReadSchemaPlan()->GetOutputSchema()->field_names(), arrow_pool_);
}
return batch_reader;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can wrap this part into a base-class function, something like ApplyProjectionBatchReader since I’ve seen this kind of logic more than three times already.

SpecialFields::ValidatePredicateFieldNames(field_names, allowed_system_fields));
arrow::FieldVector validation_fields = arrow_schema->fields();
validation_fields.insert(validation_fields.end(), allowed_system_fields.begin(),
allowed_system_fields.end());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there’s similar logic in internal_read_context.cpp as well. Maybe we can merge or reuse it.

static Result<std::shared_ptr<ReadSchemaPlan>> Create(
const std::shared_ptr<arrow::Schema>& source_schema,
const std::shared_ptr<arrow::Schema>& output_schema,
const std::set<std::string>& required_fields) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to know where you added the TODO comment about supporting a non-precise filtering mode later, where we avoid extra I/O for the fields only used in the predicate.

SpecialFields::ValidatePredicateFieldNames(predicate_fields, /*allowed_system_fields=*/{}));
PAIMON_RETURN_NOT_OK(PredicateValidator::ValidatePredicateWithSchema(
*DataField::ConvertDataFieldsToArrowSchema(table_schema->Fields()),
context->GetPredicate()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowed_system_fields seems a bit too strict. Java supports this; at least we should add a TODO explaining this inconsistency.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants